Imports System.DirectoryServices
Imports adsiClass
Imports Crypto

Partial Class wpAdminManageGroups
    Inherits System.Web.UI.Page

    Dim cr As New Crypto
    Dim ad As New adsiClass

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.lblLoginName.Text = "test\test.user1"
        'ad.userName = "test\crmadmin"
        'ad.userPassword = "a"
        ''''''''''''''''''''''''''''''''''''''
        Me.lblLoginName.Text = Session("loginName").ToString
        ad.userName = cr.Decrypt(Session("user").ToString, "key1!")
        ad.userPassword = cr.Decrypt(Session("pas").ToString, "key1!")
        ''''''''''''''''''''''''''''''''''''''
        'Me.lstAll.Items.Clear()
        'Me.lstUser.Items.Clear()

        If Not IsPostBack Then
            '''''''''Load all Groups''''''''''''''
            Dim de As DirectoryEntry = ad.GetDirectoryEntry("LDAP://" & cr.Decrypt(Session("dc").ToString, "key1!") & "")
            Dim deSearch As New DirectorySearcher()
            deSearch.SearchRoot = de
            deSearch.Filter = "((objectClass=group))"
            Dim results As SearchResultCollection = deSearch.FindAll()
            Dim i As Integer
            Dim sItems(results.Count) As String
            For i = 0 To results.Count - 1
                'Me.lstAll.Items.Add(results(i).Properties("CN").Item(0).ToString())
                sItems(i) = results(i).Properties("CN").Item(0).ToString()
            Next
            Array.Sort(sItems)
            For i = 0 To results.Count
                If sItems(i) <> "" Then
                    Me.lstAll.Items.Add(sItems(i))
                End If

            Next
            Me.lstAll.DataBind()
            ''''''''''''''''''''''''''''''''''''''''
            '''''''''''Load User's Groups'''''''''''
            Dim LoginName As String
            Dim result As SearchResult
            Dim userVal As String = Me.lblLoginName.Text.ToString
            LoginName = ad.ExtractUserNameFromLoginName(userVal.ToString)
            Dim dsh As New DirectorySearcher(de)
            dsh.SearchScope = SearchScope.Subtree
            dsh.Filter = "(&(objectClass=user) (sAMAccountName=" & LoginName & "))"
            Try
                result = dsh.FindOne()
            Catch ex As Exception
                ad.SaveTextToFile(Now.ToLongDateString & " " & Now.ToLongTimeString & vbNewLine & ex.Message.ToString & vbNewLine & ex.ToString & vbNewLine & vbNewLine & ex.StackTrace.ToString & vbNewLine & vbNewLine & "Initial Connection String --> " & de.Path.ToString & vbNewLine & "User Name--> " & LoginName & vbNewLine & "Filtered Connection String--> " & dsh.Filter.ToString & " ad user-" & ad.userName.ToString & " ad pass-" & ad.userPassword.ToString)
                Session("msg") = "Server was not accessible or wrong username/password was attempted..."
                Response.Redirect("~/wpMessage.aspx")
            End Try
            Dim userGroupsCount As Int16 = CInt(result.Properties("memberOf").Count.ToString)
            Dim userGroups(userGroupsCount) As String
            Dim j As Int16
            For j = 0 To userGroupsCount - 1
                userGroups(j) = ad.ExtractCN(de, result.Properties("memberOf").Item(j).ToString())
            Next
            Array.Sort(userGroups)
            For j = 0 To userGroupsCount
                If userGroups(j) <> "" Then
                    Me.lstUser.Items.Add(userGroups(j))
                End If
            Next
            Me.lstUser.DataBind()
            ''''''''''''''''''''''''''''''''''''''''
        End If
        
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ad.userName = cr.Decrypt(Session("user").ToString, "key1!")
        ad.userPassword = cr.Decrypt(Session("pas").ToString, "key1!")
        Dim loginName As String = ad.ExtractUserNameFromLoginName(Me.lblLoginName.Text.ToString)
        Dim result As SearchResult
        '''''''Declare Username and Password before you make this object...
        Dim de As DirectoryEntry = ad.GetDirectoryEntry("LDAP://" & cr.Decrypt(Session("dc").ToString, "key1!") & "")
        Dim dsh As New DirectorySearcher(de)
        dsh.SearchScope = SearchScope.Subtree
        dsh.Filter = "(&(objectClass=user) (sAMAccountName=" & loginName & "))"
        Try
            result = dsh.FindOne()
            ad.AddUserToGroup(de, result.Path, Me.lstAll.SelectedItem.ToString)
        Catch ex As Exception
            Session("msg") = "Server was not accessible or wrong username/password was attempted..."
            Response.Redirect("~/wpMessage.aspx")
        End Try
        'Response.Redirect("wpAdminManageGroups.aspx")
        Server.Transfer("wpAdminManageGroups.aspx")
    End Sub

    Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        ad.userName = cr.Decrypt(Session("user").ToString, "key1!")
        ad.userPassword = cr.Decrypt(Session("pas").ToString, "key1!")
        Dim loginName As String = ad.ExtractUserNameFromLoginName(Me.lblLoginName.Text.ToString)
        Dim result As SearchResult
        '''''''Declare Username and Password before you make this object...
        Dim de As DirectoryEntry = ad.GetDirectoryEntry("LDAP://" & cr.Decrypt(Session("dc").ToString, "key1!") & "")
        Dim dsh As New DirectorySearcher(de)
        dsh.SearchScope = SearchScope.Subtree
        dsh.Filter = "(&(objectClass=user) (sAMAccountName=" & loginName & "))"
        Try
            result = dsh.FindOne()
            ad.RemoveUserFromGroup(de, result.Path, Me.lstUser.SelectedItem.ToString)
        Catch ex As Exception
            Session("msg") = "Server was not accessible or wrong username/password was attempted..."
            Response.Redirect("~/wpMessage.aspx")
        End Try
        'Response.Redirect("wpAdminManageGroups.aspx")
        Server.Transfer("wpAdminManageGroups.aspx")
    End Sub

End Class
